package com.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.example.config.ConstantsConfig;
import com.example.exception.BizException;
import com.example.mapper.OptMapper;
import com.example.mapper.OrderDetailsMapper;
import com.example.mapper.OrderMapper;
import com.example.pojo.Opt;
import com.example.pojo.Order;
import com.example.pojo.OrderDetails;
import com.example.service.OrderService;
import com.example.utils.DateUtil;
import com.example.vo.CashierGoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.*;

/**
 *  订单服务接口实现
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailsMapper detailsMapper;
    @Autowired
    private OptMapper optMapper;

    @Override
    public Integer createOrder(String no,String ono,String operator) {
        //获取收银存放的结算数据
        List<CashierGoodsVo> voList = (List<CashierGoodsVo>) ConstantsConfig.cashierMap.get(no).get(ono);
        //存放订单详情对象
        List<OrderDetails> orderDetails = new ArrayList<>();
        //获取订单总金额  创建订单详情对象
        Double total_price = 0.0;
        for (CashierGoodsVo cashierGoodsVo : voList) {
            //添加
            orderDetails.add(cashierGoodsVo.toOrderDetails());
            //计算总金额
            total_price+=cashierGoodsVo.getNumber()*cashierGoodsVo.getPrice().doubleValue();
        }
        //实例化订单
        Order order = new Order(UUID.randomUUID().toString(), BigDecimal.valueOf(total_price),new Date(),operator,1);
        orderMapper.insertSelective(order);
        //更改订单详情关联订单ID;
        orderDetails.forEach(od->{
            od.setOid(order.getOid());
        });
        //实例化订单详情
        detailsMapper.insertList(orderDetails);
        return order.getOid();
    }

    @Override
    public Integer payOrder(Integer oid, List<Integer> typeids, List<BigDecimal> prices) {

        //校验
        Order order = orderMapper.selectByPrimaryKey(oid);
        if(order== null){
            throw new BizException("没有这个订单信息");
        }
        if(order.getStatus()==2){
            throw new BizException("订单已支付");
        }
        //总金额
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (BigDecimal price : prices) {
            totalPrice = totalPrice.add(price);
        }
        if(order.getPrice().compareTo(totalPrice)!=0){
            throw new BizException("支付金额和订单金额不正确");
        }
        List<Opt> opts = new ArrayList<>();

        for (int i = 0; i < typeids.size(); i++) {
            opts.add(new Opt(oid,typeids.get(i),prices.get(i)));
        }
        optMapper.insertList(opts);
        order.setStatus(2);
        return orderMapper.updateByPrimaryKeySelective(order);
    }
    @Override
    public PageInfo<Order> pageOrder(Integer pageNum, Integer pageSize, String ono, BigDecimal minPrice, BigDecimal maxPrice, String startTime, String endTime, Integer status, String operator) {
        PageHelper.startPage(pageNum,pageSize);
        endTime = StringUtils.isEmpty(endTime) ? DateUtil.getNextOneDate(new Date()) :endTime;
        minPrice = StringUtils.isEmpty(String.valueOf(minPrice)) ? BigDecimal.valueOf(0.00) :minPrice;
        maxPrice = StringUtils.isEmpty(String.valueOf(maxPrice)) ? BigDecimal.valueOf(10000.00) :maxPrice;
        List<Order> orders = orderMapper.selectByCondition(ono,minPrice,maxPrice,startTime,endTime,status,operator);
        return new PageInfo<>(orders);
    }

    @Override
    public  Order selectInfoByOid(int oid) {
        return  orderMapper.selectInfoByOid(oid);
    }
}
